#include "common.h"

/*  Defines  */

#define COUNTER_MAX     (FPS * 2)  // 2 secs 
#define SIGNAL_PTN      0xEEE3AA3AU  // "OBN" in Morse code

#define IMG_OBN_W       28
#define IMG_OBN_H       21
#define IMG_OBN_ID_MAX  3
#define IMG_SOFT_W      8
#define IMG_SOFT_H      32

/*  Local Variables  */

PROGMEM static const uint8_t imgOBN[IMG_OBN_ID_MAX][84] = { // 28x21 x3
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x10, 0x08, 0x04, 0x04, 0x02, 0x02,
        0x02, 0xFF, 0xFF, 0xA9, 0x51, 0xAA, 0x52, 0xA4, 0x48, 0x10, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x3C, 0xFF, 0xE4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA7, 0x57, 0xAA,
        0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0xC0, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x01, 0x03, 0x06, 0x0D, 0x0E, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x0A, 0x0D, 0x06,
        0x03, 0x01, 0x00, 0x00
    },{
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x10, 0x08, 0x04, 0x04, 0x02, 0x02,
        0x02, 0xFF, 0xFF, 0xA9, 0x51, 0xAA, 0x52, 0xA4, 0x48, 0x10, 0x60, 0x80, 0x00, 0xFC, 0xFC, 0xA4,
        0x54, 0xA4, 0x54, 0xA4, 0x57, 0xA4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA7, 0x57, 0xAA,
        0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0xC0, 0x3F, 0x00, 0x1F, 0x1F, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A,
        0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x0A, 0x0D, 0x06,
        0x03, 0x01, 0x00, 0x00
    },{
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x10, 0x08, 0x04, 0x04, 0x02, 0x02,
        0x02, 0xFF, 0xFF, 0xA9, 0x55, 0xA9, 0x55, 0xA9, 0x55, 0xA9, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x3C, 0xFF, 0xE4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA4, 0x54, 0xA7, 0x57, 0xA6,
        0x57, 0xA6, 0x57, 0xA6, 0x57, 0xA6, 0x04, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x01, 0x03, 0x06, 0x0D, 0x0E, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A, 0x1D, 0x1A,
        0x1D, 0x1A, 0x10, 0x1F
    }
};

PROGMEM static const uint8_t imgSoft[32] = { // 8x32
    0x7F, 0x7F, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x7F, 0x7F, 0x70, 0x7E, 0x7E, 0x70, 0x70, 0x70,
    0x3E, 0x7F, 0xF7, 0xE7, 0xE7, 0xEF, 0xFE, 0x7C, 0x3F, 0x7F, 0x78, 0x3C, 0x1E, 0x0E, 0xFE, 0xFC
};

static uint8_t  counter;
static bool     signalOn;

/*---------------------------------------------------------------------------*/
/*                              Main Functions                               */
/*---------------------------------------------------------------------------*/

void initLogo(void)
{
    counter = COUNTER_MAX;
}

MODE_T updateLogo(void)
{
    counter--;
    signalOn = (SIGNAL_PTN >> (counter - FPS / 4) / (FPS / 20)) & 1;
    arduboy.setRGBled(0, 0, signalOn * 127);
    MODE_T ret = (counter == 0) ? MODE_TITLE : MODE_LOGO;
    if (ret) {
        dprintln(F("Start " APP_TITLE " Version " APP_VERSION));
    }
    return ret;
}

void drawLogo(void)
{
    arduboy.clear();
    int shake = (COUNTER_MAX - counter) / (44 * FPS / 60);
    for (int i = 0; i < IMG_OBN_ID_MAX; i++) {
        int x = 42 + (i == shake) * signalOn;
        arduboy.drawBitmap(x, 43 - i * 21, imgOBN[i], IMG_OBN_W, IMG_OBN_H, WHITE);
    }
    arduboy.drawBitmap(72, 0, imgSoft, IMG_SOFT_W, IMG_SOFT_H, WHITE);
    arduboy.printEx(92, 53, F(APP_CODE));
    arduboy.printEx(98, 56, F("VER " APP_VERSION));
}
